package com.zheng.study.web.controller;

import cn.hutool.core.util.StrUtil;
import com.zheng.study.core.entity.TmedtechFlow;
import com.zheng.study.core.service.TmedtechFlowService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2019/2/14 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/tmedtechFlow")
public class TmedtechFlowController extends BaseController {

	@Autowired
	private TmedtechFlowService tmedtechFlowService;

	/**
	 * 获取所有列表
	 */
	@RequestMapping(value = "getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Object getList(){
		List<Object> tmedtechFlowList = tmedtechFlowService.getList();
		List<Map<String, String>> data  = tmedtechFlowService.getMapList(tmedtechFlowList);
		return JSONArray.fromObject(data).toString();
	}

	/**
	 * 获取新数据
	 */
	@RequestMapping(value = "getNewList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Object getNewList(String startDate, String endDate, String hasSeqNo){
		System.out.println("===============");
		System.out.println("startDate:" + startDate);
		System.out.println("endDate:" + endDate);
		System.out.println("hasSeqNo:" + hasSeqNo);
		List<Object> list = tmedtechFlowService.getNewList(startDate, endDate, hasSeqNo);
		List<Map<String, String>> data  = tmedtechFlowService.getNewMapList(list);
		System.out.println("获取到了"+data.size()+"条数据");
		return JSONArray.fromObject(data).toString();
	}

	/**
	 * 根据主键获取实例
	 */
	@RequestMapping(value = "getObjBySequence", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Object getObjBySequence(String flow_sequence){
		if(StrUtil.isNotBlank(flow_sequence)){
			Object o = tmedtechFlowService.getObjBySequence(flow_sequence);
			if(o!=null){
				return resultService.successWithData(tmedtechFlowService.getMap(o));
			}
		}
		return resultService.fail("未找到数据");
	}

	/**
	 * 根据主键获取process_status的值
	 */
	@RequestMapping(value = "getProcessStatusBySequence", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Object getProcessStatusBySequence(String flow_sequence){
		if(StrUtil.isNotBlank(flow_sequence)){
			String status = tmedtechFlowService.getProcessStatusBySequence(flow_sequence);
			if(StrUtil.isNotBlank(status)){
				return resultService.successWithData(status);
			}
		}
		return resultService.fail("未找到数据");
	}

	/**
	 * 根据sqe_no更新process_status
	 */
	@RequestMapping(value = "updateProcessStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Object updateProcessStatus(String flow_sequence, String status){
		if(StrUtil.isBlank(flow_sequence)){
			return resultService.fail("flow_sequence为空");
		}
		if(StrUtil.isBlank(status)){
			return resultService.fail("status为空");
		}
		int line = tmedtechFlowService.updateProcessStatus(flow_sequence, status);
		System.out.println("更新影响行数：" + line);
		return resultService.success();
	}

	/**
	 * 测试
	 */
	@RequestMapping(value = "test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public Map<String, Object> test(){
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("center_server", "成功");
		map.put("center_sql", "成功");
		try {
			tmedtechFlowService.getProcessStatusBySequence("123");
		}catch (Exception e){
			map.put("center_sql", "失败");
		}
		return map;
	}

}
